/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dal;

import client.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author mrlucky
 */
public class AccountDAL {
    
    public AccountDAL() {
    }
    
    public void insertAccount(Account acc)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.save(acc);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public Account loadAccountByUserName(String username)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Account acc = (Account) ss.createQuery("from Account a where a.useName ='"+username+"'").uniqueResult();
        ss.beginTransaction().commit();
        ss.close();
        return acc;
    }
    
    public Account loadAccountByID(int accID)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Account acc = (Account) ss.createQuery("from Account a where a.id ="+accID).uniqueResult();
        ss.beginTransaction().commit();
        ss.close();
        return acc;
    }
    
    public void updateAccount(Account acc)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.update(acc);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
}
